home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tclX6.4c / dist / tests / signal.test < prev    next >
Encoding:
Text File  |  1992-11-07  |  7.0 KB  |  267 lines

  1. #
  2. # signal.test
  3. #
  4. # Tests for the signal and kill commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: signal.test,v 2.0 1992/10/16 04:50:11 markd Rel $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. #
  22. # Determine if we have Posix signals.
  23. #
  24. set msg {}
  25. set posix 1
  26. catch {signal unblock SIGHUP} msg
  27. if {$msg == "Posix signals are not available on this system"} {
  28.     set posix 0
  29. }
  30.  
  31. Test signal-1.1 {signal tests} {
  32.     signal ignore SIGHUP
  33.     kill HUP [id process]
  34. } 0 {}
  35.  
  36. Test signal-1.2 {signal tests} {
  37.     global errorInfo
  38.     set errorInfo {}
  39.     signal error HUP
  40.     proc KillMe3 {} {kill SIGHUP [id process]}
  41.     proc KillMe2 {} {KillMe3}
  42.     proc KillMe1 {} {KillMe2}
  43.     list [catch {KillMe1} msg] $msg $errorInfo
  44. } 0 {1 {SIGHUP signal received} {SIGHUP signal received
  45.     while executing
  46. "kill SIGHUP [id process]"
  47.     (procedure "KillMe3" line 1)
  48.     invoked from within
  49. "KillMe3"
  50.     (procedure "KillMe2" line 1)
  51.     invoked from within
  52. "KillMe2"
  53.     (procedure "KillMe1" line 1)
  54.     invoked from within
  55. "KillMe1"}}
  56.  
  57. Test signal-1.3 {signal tests} {
  58.     signal error {HUP SIGTERM}
  59.     set one [list [catch {kill HUP  [id process]} msg] $msg]
  60.     set two [list [catch {kill TERM [id process]} msg] $msg]
  61.     list $one $two
  62. } 0 {{1 {SIGHUP signal received}} {1 {SIGTERM signal received}}}
  63.  
  64. Test signal-1.4 {signal tests} {
  65.     set signalWeGot {}
  66.     signal trap 1 {set signalWeGot $signalReceived} 
  67.     kill SIGHUP [id process]
  68.     signal default 1
  69.     set signalWeGot
  70. } 0 {SIGHUP}
  71.  
  72. Test signal-1.5 {signal tests} {
  73.     signal default {SIGHUP SIGINT}
  74.     signal get {SIGHUP SIGINT}
  75. } 0 {{SIGHUP {default 0}} {SIGINT {default 0}}}
  76.  
  77. Test signal-1.6 {signal tests} {
  78.     signal default SIGHUP
  79.     signal ignore  SIGINT
  80.     signal get {SIGHUP SIGINT}
  81. } 0 {{SIGHUP {default 0}} {SIGINT {ignore 0}}}
  82.  
  83. Test signal-1.7 {signal tests} {
  84.     signal trap {SIGHUP SIGINT} {error "Should not get this signal"}
  85.     signal get {SIGHUP SIGINT}
  86. } 0 [list {SIGHUP {trap 0 {error "Should not get this signal"}}} \
  87.           {SIGINT {trap 0 {error "Should not get this signal"}}} ]
  88.  
  89. Test signal-1.8 {signal tests} {
  90.     signal error {SIGHUP SIGINT}
  91.     signal get {SIGHUP SIGINT}
  92. } 0 {{SIGHUP {error 0}} {SIGINT {error 0}}}
  93.  
  94. Test signal-1.9 {signal tests} {
  95.     global errorInfo
  96.     set errorInfo {}
  97.     proc KillMe3 {} {kill SIGHUP [id process]}
  98.     proc KillMe2 {} {KillMe3}
  99.     proc KillMe1 {} {KillMe2}
  100.     signal trap SIGHUP {error "Blew it in the trap code"}
  101.     list [catch {KillMe1} msg ] $msg $errorInfo
  102. } 0 {1 {Blew it in the trap code} {Blew it in the trap code
  103.     while executing
  104. "error "Blew it in the trap code""
  105.     while executing signal trap code for SIGHUP signal
  106.     invoked from within
  107. "kill SIGHUP [id process]"
  108.     (procedure "KillMe3" line 1)
  109.     invoked from within
  110. "KillMe3"
  111.     (procedure "KillMe2" line 1)
  112.     invoked from within
  113. "KillMe2"
  114.     (procedure "KillMe1" line 1)
  115.     invoked from within
  116. "KillMe1"}}
  117.  
  118. Test signal-1.10 {signal tests} {
  119.     signal
  120. } 1 {wrong # args: signal action signalList [commands]}
  121.  
  122. Test signal-1.11 {signal tests} {
  123.     signal ignore foo
  124. } 1 {invalid signal name: foo}
  125.  
  126. Test signal-1.12 {signal tests} {
  127.     signal ignore sigint "echo foo"
  128. } 1 {command may not be specified for "ignore" action}
  129.  
  130. Test signal-1.13 {signal tests} {
  131.     signal baz sigint
  132. } 1 {invalid signal action specified: baz: expected one of "default", "ignore", "error", "trap", or "get", "block", "unblock"}
  133.  
  134. #
  135. # Complex test for the death of a child.
  136. #
  137.  
  138. proc PollSigChld {} {
  139.     global G_gotChild
  140.     set sleepCnt 0
  141.     while {!$G_gotChild} {
  142.         incr sleepCnt
  143.         if {$sleepCnt > 90} {
  144.             error "signal-1.14: SIGCHLD lost"
  145.         }
  146.         sleep 1
  147.     }
  148. }
  149.  
  150.  
  151. proc ForkChild {exitCode} {
  152.     flush stdout  ;# Not going to exec, must clean up the buffers.
  153.     flush stderr
  154.     set childPid [fork]
  155.     if {$childPid == 0} {
  156.         exit $exitCode
  157.     }
  158.     return $childPid
  159. }
  160.  
  161. if $posix {
  162.     set expect {123 {{SIGCHLD {trap 0 {global G_gotChild;set G_gotChild 1;sleep 1}}}}}
  163. } else {
  164.     set expect {123 {{SIGCHLD {default 0}}}}
  165. }
  166. set expect 
  167. Test signal-1.15 {signal tests} {
  168.     global G_gotChild
  169.     set G_gotChild 0
  170.     signal trap SIGCHLD {global G_gotChild;set G_gotChild 1;sleep 1}
  171.     set pid1 [ForkChild 123] 
  172.     PollSigChld
  173.     set status1 [wait $pid1]
  174.     list [lindex $status1 2] [signal get SIGCHLD]
  175. } 0 $expect
  176.  
  177. signal default SIGCHLD
  178.  
  179. #
  180. # Check that the signals are left in the correct state after receiving
  181. # a signal (on SIGCHLD is different if we have Posix signals).
  182. #
  183.  
  184. Test signal-1.16 {signal tests} {
  185.     global G_gotChild
  186.     set G_gotChild 0
  187.     signal trap SIGCHLD {global G_gotChild;set G_gotChild 1}
  188.     kill SIGCHLD [id process]
  189.     set gotChild1 $G_gotChild
  190.     set G_gotChild 0
  191.     kill SIGCHLD [id process]
  192.     set gotChild2 $G_gotChild
  193.     set G_gotChild 0
  194.     signal trap SIGCHLD {global G_gotChild;set G_gotChild 1}
  195.     kill SIGCHLD [id process]
  196.     set gotChild3 $G_gotChild
  197.     signal default SIGCHLD
  198.     list $gotChild1 $gotChild2 $gotChild3
  199. } 0 [list 1 $posix 1]
  200.  
  201.  
  202. Test signal-1.17 {signal tests} {
  203.     global G_gotPipe
  204.     set G_gotPipe 0
  205.     signal trap SIGPIPE {global G_gotPipe;set G_gotPipe 1}
  206.     kill SIGPIPE [id process]
  207.     set gotPipe1 $G_gotPipe
  208.     set G_gotPipe 0
  209.     kill SIGPIPE [id process]
  210.     set gotPipe2 $G_gotPipe
  211.     signal default SIGPIPE
  212.     list $gotPipe1 $gotPipe2
  213. } 0 {1 1}
  214.  
  215. if $posix {
  216.     Test signal-1.18 {signal tests} {
  217.         signal error SIGHUP
  218.         signal block SIGHUP
  219.         signal get SIGHUP
  220.     } 0 {{SIGHUP {error 1}}}
  221.  
  222.     Test signal-1.19 {signal tests} {
  223.         signal unblock SIGHUP
  224.         signal get SIGHUP
  225.     } 0 {{SIGHUP {error 0}}}
  226.  
  227.     Test signal-1.18 {signal tests} {
  228.         signal block SIGHUP
  229.         signal error SIGHUP
  230.         signal get SIGHUP
  231.     } 0 {{SIGHUP {error 1}}}
  232.  
  233.     signal unblock SIGHUP
  234.     signal default SIGHUP
  235. }
  236.  
  237.  
  238. Test signal-2.1 {kill tests} {
  239.     kill
  240. } 1 {wrong # args: kill [signal] processlist}
  241.  
  242. signal error SIGINT
  243.  
  244. Test signal-2.2 {kill tests} {
  245.     kill 2 [id process]
  246. } 1 {SIGINT signal received}
  247.  
  248. Test signal-2.3 {kill tests} {
  249.     kill INT [id process]
  250. } 1 {SIGINT signal received}
  251.  
  252. Test signal-2.4 {kill tests} {
  253.     kill SIGINT [id process]
  254. } 1 {SIGINT signal received}
  255.  
  256. Test signal-2.5 {kill tests} {
  257.     kill 10000 [id process]
  258. } 1 {invalid signal}
  259.  
  260. Test signal-2.6 {kill tests} {
  261.     kill SIGFOO [id process]
  262. } 1 {invalid signal}
  263.  
  264. Test signal-2.7 {kill tests} {
  265.     kill 0 [id process]
  266. } 0 {}
  267.